package com.foreknow.controller;

import com.foreknow.bean.GuestBook;
import com.foreknow.service.GuestBookService;
import com.foreknow.service.GuestBookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String method = req.getParameter("q");
       if ("list".equals(method)){
           list(req, resp);
       }else if ("delete".equals(method)){
           delete(req,resp);
       }else if ("add".equals(method)){
           add(req, resp);
       }else if ("update".equals(method)){
            update(req, resp);
       }else if ("jumpAddMessage".equals(method)){
           jumpAddMessage(req,resp);
       }else if ("jumpUpdateMessage".equals(method)){
           jumpUpdateMessage(req,resp);
       }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
//        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
//        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //调用service的方法,查询所有留言的数据
        GuestBookService service = new GuestBookServiceImpl();
        List<Object> list = service.showAllBook();
        //如何将list集合保存到一个地方,转发之后就可以在list.jsp中方便获取,需要将list保存到服务器端
        req.setAttribute("list",list);
        //当前的Servlet中如何跳转到jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("text/html;charset=utf-8");
        GuestBook gb = new GuestBook();
        gb.setName(req.getParameter("name"));
        gb.setPhone(req.getParameter("phone"));
        gb.setEmail(req.getParameter("email"));
        gb.setTitle(req.getParameter("title"));
        gb.setContent(req.getParameter("content"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        gb.setCreatedtime(simpleDateFormat.format(date));
        GuestBookService guestBookService = new GuestBookServiceImpl();
        boolean isRight = guestBookService.addBook(gb);
        if (isRight){
//            req.getRequestDispatcher("/manager?q=list").forward(req,resp);
            resp.sendRedirect(req.getContextPath()+"/manager?q=list");
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("text/html;charset=utf-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        GuestBookService service = new GuestBookServiceImpl();
        boolean isRight = service.deleteBook(id);
        if (isRight) {
//            req.getRequestDispatcher("/manager?q=list").forward(req, resp);
            resp.sendRedirect(req.getContextPath()+"/manager?q=list");
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        GuestBookService guestBookService = new GuestBookServiceImpl();
        GuestBook gb = guestBookService.selectById(id);
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String createdtime = simpleDateFormat.format(date);
        boolean isRight = guestBookService.updateBook(gb,name,phone,email,title,content,createdtime);
        if (isRight){
//            req.getRequestDispatcher("/manager?q=list").forward(req,resp);
            resp.sendRedirect(req.getContextPath()+"/manager?q=list");
        }
    }

    /**
     * 从list.jsp跳转到AddMessage.jsp
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void jumpAddMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.getRequestDispatcher("/AddMessage.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/AddMessage.jsp");
    }

    protected void jumpUpdateMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        GuestBookService guestBookService = new GuestBookServiceImpl();
        GuestBook guestBook = guestBookService.selectById(id);
        req.setAttribute("guestbook",guestBook);
//        req.getRequestDispatcher("/UpdateMessage.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/UpdateMessage.jsp");
    }
}














